<?php
/**
 * Introduction: 发挥地址设置
 * @author: 杨陈鹏
 * @email: yangchenpeng@cdlinglu.com
 */

namespace Admin\Controller\Setting;


use Common\Controller\Admin\CommonController;

class SendAddressController extends CommonController
{

    /**
     * Introduction: 发货地址列表
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function lists()
    {

        $logic = D('Common/SendAddress','Logic');

        $data = $logic::getData();

        $this->data = $data['data'];

        $this->display();

    }
    /**
     * Introduction: 新增发货地址
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function create()
    {

        $data = I('post.data');

        if ( $data ) {

            $logic = D('Common/SendAddress','Logic');

            $this->ajaxReturn($logic::create($data));

        }

        $this->display();

    }

    /**
     * Introduction: 新增发货地址
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function setDefault()
    {


        $id = I('post.data');

        $id = $id['id'];

        $logic = D('Common/SendAddress','Logic');

        $logic::$model->where(['is_default' => 1])->save(['is_default' => 2]);

        $this->ajaxReturn($logic::update([ 'id' => $id ],[ 'is_default' => 1 ]));

    }
    /**
     * Introduction: 删除
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function delete()
    {


        $id = I('post.data');

        $id = $id['id'];

        $logic = D('Common/SendAddress','Logic');

        if($logic::$model->where([ 'id' => $id ])->delete())
            $this->ajaxReturn(['code' => 200, 'msg' => '删除成功']);


        $this->ajaxReturn(['code' => 300, 'msg' => '删除失败']);



    }
    /**
     * Introduction: 修改
     * @author: 杨陈鹏
     * @email: yangchenpeng@cdlinglu.com
     */
    public function update()
    {


        $data = I('post.data');



        $this->id = I('post.id');


        $logic = D('Common/SendAddress','Logic');


        if ( $data ) {

            $where['id'] = $data['id'];

            unset($data['id']);

            $this->ajaxReturn($logic::update($where,$data));

        }

        $data = $logic::getOneData($this->id);

        $this->data = $data['data'];

        $this->display();

    }
}